package com.guanyong.admin.controllers.api;

import com.guanyong.model.HistoryTodayModel;
import com.guanyong.services.HistoryTodayService;
import com.guanyong.utils.Comm.ResultCode;
import com.guanyong.utils.Comm.ResultObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * Created by mxk on 2017/6/6.
 */
@RestController
@RequestMapping("/api/histroy/")
public class HistroyApi {
    @Autowired
    HistoryTodayService historyTodayService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    ResultObject<HashMap<String,Object>> getMessageList(HttpServletRequest request,
                                                         @RequestParam Integer show,
                                                         @RequestParam String  date,
                                                         @RequestParam int pno,
                                                         @RequestParam int pagesize,
                                                         @RequestParam int allcount) {
        ResultObject<HashMap<String,Object>> result = new ResultObject<HashMap<String,Object>>();
        HistoryTodayModel historyTodayModel =new HistoryTodayModel();
        HashMap<String,Object> hashMap=
                historyTodayService.GetHistoryTodayList(historyTodayModel,
                pno,
                pagesize,allcount);
        result.setResult(hashMap);
        return result;
    }//updatestate

    @RequestMapping(value = "updatestate", method = RequestMethod.POST)
    ResultObject<Integer> UpdateState(HttpServletRequest request,
                                      @RequestParam Integer fid,
                                      @RequestParam Integer state){
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        HistoryTodayModel historyTodayModel =new HistoryTodayModel();
        historyTodayModel.setfId(fid);
        historyTodayModel.setfShow(state);

        Integer res=historyTodayService.UpdateHistoryToday(historyTodayModel);
        if (res >-1){
            resultObject.setCode(ResultCode.OperateOk);
        }
        else{
            resultObject.setCode(ResultCode.OperateFail);
        }
        return  resultObject;
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    ResultObject<Integer> addHistroy(HttpServletRequest request,
                                      @RequestParam Integer  history_classtype,
                                      @RequestParam String history_objectid,
                                      @RequestParam String history_keyword,
                                      @RequestParam String history_date){
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        HistoryTodayModel historyTodayModel=new HistoryTodayModel();
        historyTodayModel.setfObjectid(history_objectid);
        historyTodayModel.setfClasstype(history_classtype);
        historyTodayModel.setfKeyword(history_keyword);
        historyTodayModel.setfDate(history_date);
        HistoryTodayModel historyTodayModel1=historyTodayService.GetHistoryByDate(history_date,
                history_classtype,history_objectid);
        if (historyTodayModel1!=null){
            resultObject.setCode(ResultCode.FiledRepeat);
        }
        else{
            Integer res=historyTodayService.AddHistoryToday(historyTodayModel);
            if (res >-1){
                resultObject.setCode(ResultCode.OperateOk);
            }
            else{
                resultObject.setCode(ResultCode.OperateFail);
            }
        }

        return  resultObject;
    }


    @RequestMapping(value = "update", method = RequestMethod.POST)
    ResultObject<Integer> UpdateState(HttpServletRequest request,
                                      @RequestParam Integer  history_id,
                                      @RequestParam String history_keyword){
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        HistoryTodayModel historyTodayModel=new HistoryTodayModel();
        historyTodayModel.setfId(history_id);
        historyTodayModel.setfKeyword(history_keyword);
        Integer res=historyTodayService.UpdateHistoryToday(historyTodayModel);
        if (res >-1){
            resultObject.setCode(ResultCode.OperateOk);
        }
        else{
            resultObject.setCode(ResultCode.OperateFail);
        }
        return  resultObject;
    }
}
